<template>
  <base-breadcrumb>
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <a-card :bordered="false">
      <!-- <title-name title="列表" /> -->
      <list-table
        ref="table"
        class="export-table"
        :columns="columns"

        :request="loadData"
        :showTotalSize="false"
        :pageKeys="['pageNum', 'pageSize', 'total']"
      >
        <!-- <template slot="prefix" slot-scope="ref">
          <a-button type="primary" @click="handleEdit('add', {})">编辑</a-button>
        </template> -->
        
        <template slot="status" slot-scope="text, record">
          <!-- blue | green | red | yellow | grey -->
          <!-- // 0:待提交审核;1:审核中; 2:审核通过;3审核不通过;9:撤销 -->
            <list-status :color="{0: 'yellow', 1: 'blue', 2: 'green', 3: 'red', 9: 'grey'}[record.status]" conversion="en" :text="text"></list-status>
        </template>
        <template slot="operation" slot-scope="text, record">
          <MoreButton>
          <a @click="handleEdit('look', record)" v-btnPermission="'crm_003_loo_01'">查看</a>
          
          <template v-if="record.status == 0 || record.status == 3" v-btnPermission="'crm_003_edi_02'">
            <a @click="handleEdit('edit', record)" v-btnPermission="'crm_003_edi_02'">编辑</a>
          </template>
          
          <template v-if="record.status == 1" v-btnPermission="'crm_003_edi_02'">
            <!-- 1-删除,2-撤销 -->
            <a @click="handleDel('2', record)" v-btnPermission="'crm_003_rev_01'">撤回</a>
          </template>
          
          <template v-if="record.status != 0 && record.status != 9">
            <approval-button :id="record.id" />
          </template>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>
<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import listStatus from '@/components/list_status/list_status'

// import { getWbsTemplateList } from '@/api/template'
import { getMaintenancePlanList, delMaintenancePlan } from '@/api/complaint/maintenanceplan'

export default {
  name: 'a' + Date.now(),
  components: {
    listStatus,
  },
  data: function () {
    return {
      loading: false,

      pathName: this.$route.path,
      queryParam: {},
      
      loadData: (parameter) => {
        let that = this
        const requestParameters = Object.assign({}, parameter, { param: this.queryParam })
        // console.log(requestParameters)
        // return new Promise((resove, reject) => {
        //   resove({ code: 200, data: [] })
        // })
        return getMaintenancePlanList(requestParameters).then((res) => {
          if (res.code == 0) {
            return { code: 200, ...res.data, data: res.data.list }
          }
          return { code: 200, data: [] }
        })
      },

      searchArr: [
      {
        name: '所属单位',
        valueKey: 'searchQuery',
        type: OrgTreeSelect,
      },
        // {
        //     name: '所属分公司',
        //     valueKey: 'branchComId',
        //     type: SelectSubCompany
        // },
        // {
        //     name: '项目名称',
        //     type: SelectProject,
        //     keyValue: 'branchComId',
        //     valueKey: 'projectId'
        // },
        {
          name: '维保方案编号',
          value: 'maintenancePlanCode',
          type: 'input',
        },
        {
          name: '实施分包单位',
          value: 'implementSubOrg',
          type: 'input',
        },
        {
          name: '负责人',
          value: 'responsible',
          type: 'input'
        },
        {
          name: '维修计划完成时间',
          value: 'planCompletedTime',
          type: 'dateRange',
          attr: {
              rangeFormat: ['planCompletedTimeBegin', 'planCompletedTimeEnd']
          }
        },
        {
          name: '维保发起人',
          value: 'maintenancePeople',
          type: 'input'
        },
        {
          name: '维保发起时间',
          value: 'maintenanceTime',
          type: 'dateRange',
          attr: {
              rangeFormat: ['maintenanceTimeBegin', 'maintenanceTimeEnd']
          }
        },
        {
            name: '状态',
            value: 'status',
            type: 'select',
            data: [
              // 0:待提交审核;1:审核中; 2:待业主反馈;3审核不通过;4:已闭合;9:撤销
              { label: '待提交审核', value: '0' },
              { label: '审核中', value: '1' },
              { label: '审核通过', value: '2' },
              { label: '审核不通过', value: '3' }
            ]
        }
      ],
      columns: [
        // {
        //   title: '序号',
        //   dataIndex: 'index',
        //   scopedSlots: { customRender: 'index' },
        //   width: 70
        // },
        {
          title: '项目名称',
          dataIndex: 'projectName',
          width: 220
        },
        {
          title: '所属分公司',
          dataIndex: 'branchComName',
          width: 160
        },
        {
          title: '维保方案编号',
          dataIndex: 'maintenancePlanCode',
          width: 160
        },
        {
          title: '预计维保金额',
          dataIndex: 'maintenanceFee',
          width: 160
        },
        {
          title: '实施分包单位',
          dataIndex: 'implementSubOrg',
          width: 160
        },
        {
          title: '负责人',
          dataIndex: 'responsible',
          width: 160
        },
        {
          title: '维修计划完成时间',
          dataIndex: 'planCompletedTime',
          width: 160
        },
        {
          title: '维保发起人',
          dataIndex: 'maintenancePeople',
          width: 160
        },
        {
          title: '维保发起时间',
          dataIndex: 'maintenanceTime',
          width: 160
        },
        {
          title: '状态',
          dataIndex: 'maintenancePlanStatus',
          scopedSlots: { customRender: 'status' },
          width: 160
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 200,
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
    }
  },
  created () {
  },
  mounted() {
    console.log(this.STATUS)
  },
  methods: {
    /**
     * 业态查询（已弃用）
     */
    getWbsTemplateList() {
      getWbsTemplateList({ pageNo: 1, pageSize: 2000 }).then((res) => {
        let arr = []
        res.data.forEach((item) => {
          arr.push({
            label: item.formatName,
            value: item.formatId
          })
        })
        this.searchArr[2].data = arr
      })
    },
    search (val) {
      this.queryParam = val
      this.$refs.table.refresh()
    },
    handleDel (type, record) {
      let that = this
      let msg = type == 1 ? '删除' : '撤销';
      this.$confirm({
        title: '温馨提示',
        content: `确定是否${msg}？`,
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          return delMaintenancePlan({ type, id: record.id })
            .then((res) => {
              that.$message.success(`${msg}成功`)
              that.$refs.table.refresh()
            }).catch(err => {
              console.log(err)
            })
        }
      })
    },

    handleEdit (type, record) {
      this.$router.push({
        path: '/complaint/Maintenance/planDetail',
        query: {
          id: record && record.id || '',
          type,
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
